package com.intellagric.service.traceability.Impl.trace_invest;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.intellagric.common.pojo.LayuiDataGridResult;
import com.intellagric.common.pojo.ResponseMessage;
import com.intellagric.dao.traceability.InvestDao;
import com.intellagric.mapper.TraceInvestMapper;
import com.intellagric.module.traceability.InvestRecordVo;
import com.intellagric.module.traceability.InvestVo;
import com.intellagric.pojo.TraceInvest;
import com.intellagric.pojo.TraceInvestExample;
import com.intellagric.service.traceability.trace_invest.InvestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: zhy
 * @email: 1269679946@qq.com
 * @Date: 2018/11/7
 * @DESC： 投入品管理服务
 */
@Service
public class InvestServiceImpl implements InvestService {

    @Autowired
    private TraceInvestMapper investMapper;

    @Autowired
    private InvestDao investDao;

    /**
     * 添加投入品信息
     * @param invest
     * @return ResponseMessage
     */
    @Override
    public ResponseMessage addInvest(TraceInvest invest) {
        if (investMapper.insertSelective(invest) == 1) {
            return ResponseMessage.success();
        } else {
            return ResponseMessage.fail();
        }
    }

    /**
     * 删除投入品信息
     * @param id
     * @return ResponseMessage
     */
    @Override
    public ResponseMessage deleteInvest(String id) {
        if (investMapper.deleteByPrimaryKey(id) == 1) {
            return ResponseMessage.success();
        } else {
            return ResponseMessage.fail();
        }
    }

    /**
     * 修改投入品信息
     * @param invest
     * @return ResponseMessage
     */
    @Override
    public ResponseMessage editInvest(TraceInvest invest) {
        if (investMapper.updateByPrimaryKeySelective(invest) == 1) {
            return ResponseMessage.success();
        } else {
            return ResponseMessage.fail();
        }
    }

    /**
     * 查询投入品信息
     * @param id
     * @return TraceInvest
     */
    @Override
    public TraceInvest getInvestById(String id) {
        return investMapper.selectByPrimaryKey(id);
    }

    /**
     * 查询投入品信息列表
     * @return LayuiDataGridResult
     */
    @Override
    public LayuiDataGridResult getInvestList() {
        TraceInvestExample investExample = new TraceInvestExample();
        investExample.createCriteria().andIdIsNotNull();
        List<TraceInvest> investList = investMapper.selectByExample(investExample);
        return LayuiDataGridResult.success().add(investList,investList.size());
    }

    /**
     * 分页查询投入品信息列表
     * @return LayuiDataGridResult
     */
    @Override
    public LayuiDataGridResult getInvestPage(int page, int rows) {
        PageHelper.startPage(page,rows);
        TraceInvestExample investExample = new TraceInvestExample();
        investExample.createCriteria().andIdIsNotNull();
        List<TraceInvest> investList = investMapper.selectByExample(investExample);
        return LayuiDataGridResult.success().add(investList,investList.size());
    }
    /**
     * 分页查询投入品信息列表
     * @return LayuiDataGridResult
     */
    @Override
    public LayuiDataGridResult getInvestPage(int page, int rows,String createBy) {
        PageHelper.startPage(page,rows);
        TraceInvestExample investExample = new TraceInvestExample();
        investExample.createCriteria().andIdIsNotNull().andCreateByEqualTo(createBy);
        List<TraceInvest> investList = investMapper.selectByExample(investExample);
        return LayuiDataGridResult.success().add(investList,investList.size());
    }

    /**
     * 根据Vo类型查询投入品信息列表
     * @return LayuiDataGridResult
     */
    @Override
    public LayuiDataGridResult getInvestByVo(InvestVo investVo) {
        List<TraceInvest> investList = investDao.selectInvestByVo(investVo);
        return LayuiDataGridResult.success().add(investList,investList.size());
    }

    @Override
    public LayuiDataGridResult queryByVo(int page, int limit, InvestVo vo) {
        PageHelper.startPage(page,limit);
        List<TraceInvest> list=investDao.selectInvestByVo(vo);
        return LayuiDataGridResult.success().add(list,new PageInfo(list).getTotal());
    }
}
